$def with (keys, top_books_from_author, formdata=None)

<script type="text/javascript">
window.q.push(function(){
    \$('#save').click(function(){
        var n = \$("#mergeForm input[type=radio]:checked").length;
        if (n == 0) {
            \$('#noMaster').dialog('open');
            return false;
        } else {
            \$('#confirmMerge').dialog('open');
            return false;
        };
    });
    \$("div.radio:first input[type=radio]").attr("checked","checked");
    \$("div.checkbox:first input[type=checkbox]").attr("checked","checked");
    \$("div.author:first").addClass("master");
    \$("#include input[type=radio]").mouseover(function(){
        \$(this).parent().parent().addClass("mouseoverHighlight",300);
    });
    \$("#include input[type=radio]").mouseout(function(){
        \$(this).parent().parent().removeClass("mouseoverHighlight",100);
    });
    \$("#include input[type=radio]").click(function(){
        var previousMaster = \$(".merge").find("div.master");
        previousMaster.removeClass("master mergeSelection");
        previousMaster.find("input[type=checkbox]").attr('checked', false);
        \$(this).parent().parent().addClass("master");
        \$(this).parent().parent().find("input[type=checkbox]").attr('checked', true);
    });
    \$("#include input[type=checkbox]").on("change", function() {
        if (!\$(this).parent().parent().hasClass("master")) {
            if (\$(this).is(":checked")) {
                \$(this).parent().parent().addClass("mergeSelection");
            } else {
                \$(this).parent().parent().removeClass("mergeSelection");
            }
        }
    });
});
</script>

$var title: $_("Merge Authors")

<div id="contentHead">
    <h1>$_("Merge Authors")</h1>
</div>

<div id="contentBody">

$if not keys:
    $# TODO: improve this message. This error comes when no author keys are passed as url parameters.
    <div class="note">$_("No authors selected.")</div>

$if formdata:
    $if not formdata.master:
        <div class="note">$_("Please select a master author record.")</div>
    $if len(formdata.selected) == 0:
        <div class="note">$_("Please select some authors to merge.")</div>

<ol class="sansserif">
    <li>$_('Select a "master" record that best represents the author -')
        <input type="radio" name="radio" id="radio" checked="checked"/>
    </li>
    <li>$_('Select author records which should be merged with the master')
        -
        <input type="checkbox" name="checkbox" id="checkbox" checked="checked" />
    </li>
    <li>$_('Press MERGE AUTHORS.')
    </li>
</ol>

<div id="noMaster" title="$_('No Master record')" style="text-align:left;">
    <p>$_('You must select a master record to point the duplicates toward.')</p>
</div>

<div id="confirmMerge" title="$_('Please Be Careful...')" style="text-align:left;">
    <p class="merge">$:_('<b>Are you sure</b> you want to merge these records?')</p>
</div>

<form method="POST" id="mergeForm" name="mergeForm">
    <input type="hidden" name="merge" value="true"/>
    <div class="merge" id="include">

        <div class="entry header">
            <div class="data input smaller center" style="background-color:#fffdcd;min-height:25px;">$_('Master')</div>
            <div class="data input smaller center"><strong>$_('Merge')</strong></div>
            <div class="data record smaller"><strong>$_('Authors')</strong></div>
            <div class="data count">&nbsp;</div>
        </div>

        $if keys:
            $ master = formdata and formdata.master or keys[0]
        $else:
            $ master = None

        $for k in keys:
            $ a = get_document('/authors/' + k)
            $ top = top_books_from_author(k)
            <div class="entry author">
                <div class="data input radio">
                    $:radio_input(checked=(k==master), name='master', value=k)
                </div>
                <div class="data input checkbox">
                    <input type="checkbox" value="$k" name="merge_key" id="$k" $cond(formdata and k in formdata.selected, 'checked="checked"', '')/>
                </div>
                <div class="data record">
                    <label for="$k">
                        <span class="name">$a.name</span>
                        $if a.birth_date or a.death_date:
                            <span class="metaDate" title="birth/death date">$a.get('birth_date', '') - $a.get('death_date', '')</span>
                        $elif a.date:
                            <span class="metaDate">$a.date</span>
                    </label>
                    <ul>
                    $for doc in top['books']:
                        <li><a href="$doc.key" target="new" title="$_('Open in a new window')">$doc.title</a> <span class="smaller">$ungettext('1 edition', '%(count)d editions', doc.edition_count, count=doc.edition_count),
                        $if doc.get('first_publish_year'):
                            <span title="$_('First published in')">$doc.first_publish_year</span>
                        </span></li>
                    </ul>
                </div>
                <div class="data count">
                    <a href="/authors/$k" target="new" title="$_('Visit this author\'s page in a new window')">$ungettext("1 work", "%(count)d works", top['total'], count=top['total'])</a>
                </div>
            </div>
    </div>

<p>
<button id="save" class="larger" value="Merge Authors">$_('Merge Authors')</button>
&nbsp;&nbsp;
<a href="javascript:history.go(-1);" class="small red sansserif">$_('Cancel')</a>
</p>

</form>

</div>
